﻿using System.Threading;
using System.Threading.Tasks;
using UpperComputer.Application;
using UpperComputer.Core.Extensions;
using UpperComputer.WPF.Entry.Common;
using YFurion.Session;

namespace UpperComputer.WPF.Entry.ViewModels
{
    public class UserPasswordViewModel : OperationViewModel
    {
        private readonly IBaseUserService _userService;

        public ChangePasswordNativeDto UserPassword { get; } = new ChangePasswordNativeDto();

        public UserPasswordViewModel(IBaseUserService userService, IYSession ySession)
        {
            _userService = userService;
            UserPassword.Id = Id > 0 ? Id : ySession.SubjectId;
        }

        protected override async Task OkAsync(object parameter, CancellationToken cancellationToken)
        {
            if (!UserPassword.IsValidated)
            {
                return;
            }
            await TryExecuteCommandAysnc(async cancellationToken =>
            {
                ChangePasswordDto input = new ChangePasswordDto
                {
                    Id = UserPassword.Id,
                    OldPassword = UserPassword.OldPassword.GetMD5SuffixPassword(),
                    NewPassword = UserPassword.NewPassword.GetMD5SuffixPassword()
                };
                await _userService.ChangePassword(input, cancellationToken);
                CloseRootDialog();
            }, cancellationToken);
        }
    }
}
